﻿using Microsoft.Extensions.DependencyInjection;
using QPlcPlayer.Models;
using QPlcPlayer.Services;
using S7.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QPlcPlayer
{
	public partial class TestPLCForm : Form
	{
		
		public static List<QDataType> DataTypes = new List<QDataType>
		{
			new QDataType{Name="BOOL",DataType=QDataTypeEnum.BOOL},
			new QDataType{Name="UNIT",DataType=QDataTypeEnum.UNIT},
			new QDataType{Name="USINT",DataType=QDataTypeEnum.USINT},
			new QDataType{Name="CHAR",DataType=QDataTypeEnum.CHAR},
			new QDataType{Name="DWORD",DataType=QDataTypeEnum.DWORD},
			new QDataType{Name="DINT",DataType=QDataTypeEnum.DINT},

		};

		public static VarType[] ValueTypes = new VarType[]
		{
			VarType.Byte,
			VarType.Int,
			VarType.Int,
			VarType.String,
			VarType.DWord,
			VarType.DInt
		};

		private AgvCar agvCar;
		private QPlcManager manager;
		private StringBuilder SB;
		public TestPLCForm()
		{
			InitializeComponent();
			agvCar=AppRuntime.GetService().GetRequiredService<AgvCarService>().GetAgvCarByNumber(AppRuntime.CarNumber);
			this.CenterToParent();
			InitForm();
			manager = new QPlcManager(agvCar);
			SB = new StringBuilder();
		}


		private void InitForm()
		{
			txtAddress.Text = "DB1000.DBX0.0";
			txtAddress_0.Text = "DB1000.DBX0.0";

			comboBox1.ValueMember = "DataType";
			comboBox1.DisplayMember = "Name";
			comboBox1.Items.AddRange(DataTypes.ToArray());
			comboBox1.SelectedIndex = 0;

		


		}

		/// <summary>
		/// 读取数据
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button1_Click(object sender, EventArgs e)
		{

			
			var address = txtAddress.Text.Trim();
			if (string.IsNullOrWhiteSpace(address))
			{
				MessageBox.Show("请输入地址");
				return;
			}
			var array = address.Split('.').ToList();
			if (array.Count != 3)
			{
				MessageBox.Show("输入的地址不正确");
				return;
			}

			try
			{
				var db = int.Parse(array[0].Replace("DB", ""));
				var startAddr = int.Parse(array[1].Replace("DBX", ""));
				var count = byte.Parse(array[2]);
				var varType = ValueTypes[comboBox1.SelectedIndex];

				var res = manager.GetPlc().Read(DataType.DataBlock, db, startAddr, varType, 1, count);
				SB.AppendLine(res.ToString());
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
				return;
			}
			//var xx = plc.Read(DataType.DataBlock, 1000, 14, VarType.String, 36);	
			



			if (SB.Length > 50)
			{
				SB.Remove(0, 1);
			}
			textBox1.Text = SB.ToString();

		}

		/// <summary>
		/// 写入数据
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button2_Click(object sender, EventArgs e)
		{
			var address = txtAddress.Text.Trim();
			if (string.IsNullOrWhiteSpace(address))
			{
				MessageBox.Show("请输入地址");
				return;
			}
			var array = address.Split('.').ToList();
			if (array.Count != 3)
			{
				MessageBox.Show("输入的地址不正确");
				return;
			}
			var obj = txtInput.Text.Trim();
			if (string.IsNullOrWhiteSpace(obj))
			{
				MessageBox.Show("输入的值不能为空");
				return;
			}
			try
			{
				var db = int.Parse(array[0].Replace("DB", ""));
				var startAddr = int.Parse(array[1].Replace("DBX", ""));
				var count = byte.Parse(array[2]);
				var varType = ValueTypes[comboBox1.SelectedIndex];
			
			    manager.GetPlc().Write(DataType.DataBlock, db, startAddr, obj);
				
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				return;
			}

		}
	}
}
